{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content animated">
    <div class="row">
        <div class="col-sm-12">
            <form id="form" class="form-horizontal" method="post" action="{:\\think\\Url::build('run')}">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h2 style="font-weight: 600">代码自动生成器</h2>
                    </div>
                    <div class="ibox-content">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">从数据表生成：</label>
                            <div class="col-sm-4">
                                <select class="form-control db-table">
                                    <option value="">不从数据表生成</option>
                                    {foreach name='tables' item='table'}
                                    <option value="{$table}">{$table}</option>
                                    {/foreach}
                                </select>
                            </div>
                            <div class="input-check col-sm-4">
                                <button class="btn btn-success btn-sm db-jump" type="button" title="点击此项选择从数据库生成字段">
                                    生成字段
                                </button>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">生成文件：</label>
                            <div class="col-sm-4">
                                <select name="file" class="form-control">
                                    <option value="all">默认生成文件（all）</option>
                                    <option value="controller">控制器（controller）</option>
                                    <option value="model">模型（model）</option>
                                    <option value="validate">验证器（validate）</option>
                                    {if condition='!$Request.param.table'}
                                    <option value="table">数据表（table）</option>
                                    {/if}
                                    <option value="add">添加页（add.html）</option>
                                    <option value="edit">编辑页（edit.html）</option>
                                    <option value="index">搜索页（index.html）</option>
                                    <option value="indexajax">列表页（indexajax.html）</option>
                                    <option value="config">配置文件（config.php）</option>
                                    <option value="dir">目录（dir）</option>
                                </select>
                            </div>
                            <div class="input-check col-sm-4"></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="text-danger">*</span>模块：</label>
                            <div class="col-sm-4">
                                <input class="form-control" type="text" name="module" value="{$Request.module}"
                                       placeholder="必填，默认为当前模块"
                                       title="默认为当前模块" required>
                            </div>
                            <div class="input-check col-sm-4"></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">控制器：</label>
                            <div class="col-sm-4">
                                <input class="form-control" type="text" name="controller"
                                       value="{$controller ?? ''}"
                                       placeholder="字母，驼峰式，例如AdminGroup，前面可加小写目录"
                                       title="正则：/^([a-z][a-z0-9]*\.)*[A-Z]\w*$/，比如one.Demo或者Demo，注意大小写">
                            </div>
                            <div class="input-check col-sm-4"></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="text-danger">*</span>中文标识：</label>
                            <div class="col-sm-4">
                                <input class="form-control" type="text" name="title" placeholder="必填，用于标题和注释，中文（例如 分组）"
                                       required>
                            </div>
                            <div class="input-check col-sm-4"></div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label"><span class="text-danger">*</span>主键名：</label>
                            <div class="col-sm-4">
                                <input class="form-control" type="text" name="table_index"
                                       placeholder="必填，用于控制器设置主键，或新建数据表" required>
                            </div>
                            <div class="input-check col-sm-4"></div>
                        </div>
                    </div>
                    <div class="ibox float-e-margins">
                        <div class="ibox float-e-margins">
                            <div class="ibox-title">
                                <h5>表单信息 ：</h5>
                            </div>
                            <div class="ibox-content table-form">
                                <div><span class="text-info"> 提示：可以拖动单元格对表格进行排序</span></div>
                                <div class="data-table table-responsive">
                                    <table class="table table-bordered table-hover" style="width: 100%;">
                                        <thead>
                                        <tr class="table-text-c">
                                            <th style="width: 190px;" rowspan="2" title="删除后不可恢复，谨慎操作">
                                                操作<br>
                                                <a href="javascript:;" class="label label-success op-add"
                                                   data-type="form"
                                                   data-header="1">增加一栏</a>
                                            </th>
                                            <th style="width: 600px;" colspan="6" title="字段配置信息">字段</th>
                                            <th style="width: 330px;" colspan="4" title="字段使用位置">使用位置</th>
                                            <th style="width: 170px;" colspan="2" title="自动生成搜索项">搜索</th>
                                            <th style="width: 90px;" rowspan="2" title="勾选后自动给字段添加排序功能">排序</th>
                                            <th style="width: 80px;" rowspan="2" title="勾选后在验证器和前端生成必填校验规则">是否必填</th>
                                        </tr>
                                        <tr class="table-text-c">
                                            <th style="width: 120px;" title="中文描述，编辑页为对应label标签内容，首页对应表头内容"><span
                                                    class="text-danger">*</span> 标题
                                            </th>
                                            <th style="width: 120px;" title="一般为对应数据库字段的名称"><span
                                                    class="text-danger">*</span>
                                                名称
                                            </th>
                                            <th style="width: 120px;" title="自动生成编辑页相应的表单控件"><span
                                                    class="text-danger">*</span>
                                                类型
                                            </th>
                                            <th style="width: 120px;"><span
                                                    class="text-danger">*</span>
                                                最大长度
                                            </th>
                                            <th style="width: 120px;"
                                                title="只针对select,radio,checkbox控件,支持变量和配置值，例如 {foo}-对应conf.foo对应的配置项，生成foreach循环 | 1:值一#2:值二#3:值三 | 空值的默认标签名">
                                                选项值
                                            </th>
                                            <th style="width: 120px;" title="字段编辑页默认值">默认值</th>
                                            <th style="width: 80px;" title="使用在列表页中">列表页</th>
                                            <th style="width: 80px;" title="使用在列表页中">显示排序</th>
                                            <th style="width: 90px;" title="使用在添加修改页中">添加修改页</th>
                                            <th style="width: 80px;" title="使用在列表页中">显示排序</th>
                                            <th style="width: 80px;" title="勾选后自动生成控制器筛选项和前端搜索框">表单搜索</th>
                                            <th style="width: 90px;" title="select的取值为字段中的选项值">搜索类型</th>
                                        </tr>
                                        </thead>
                                        <tbody id="tbody-form">
                                        <tr class="table-text-c">
                                            <td>
                                                <a href="javascript:;" class="label label-success op-add"
                                                   data-type="form">增加</a>
                                                <a href="javascript:;" class="label label-danger op-delete"
                                                   title="删除后不可恢复，谨慎操作">删除</a>
                                            </td>
                                            <td title="中文描述，编辑页为对应label标签内容，首页对应表头内容">
                                                <input type="text" class="form-control form-title" placeholder="中文描述"
                                                       name="form[0][title]">
                                            </td>
                                            <td title="一般为对应数据库字段的名称">
                                                <input type="text" class="form-control form-name" placeholder="字段，字母"
                                                       name="form[0][name]">
                                            </td>
                                            <td title="自动生成编辑页相应的表单控件">
                                                <select class="form-control" name="form[0][type]">
                                                    <option value="text">text</option>
                                                    <option value="select">select</option>
                                                    <option value="radio">radio</option>
                                                    <option value="textarea">textarea</option>
                                                    <option value="checkbox">checkbox</option>
                                                    <option value="password">password</option>
                                                    <option value="number">number</option>
                                                    <option value="date">date</option>
                                                </select>
                                            </td>
                                            <td>
                                                <input type="text" class="form-control form-length" placeholder="字段最大长度"
                                                       name="form[0][length]">
                                            </td>
                                            <td title="只针对select,radio,checkbox控件,支持变量和配置值，例如 {foo}-对应conf.foo对应的配置项，生成foreach循环 | 1:值一#2:值二#3:值三 | 空值的默认标签名">
                                                <input type="text" class="form-control" placeholder="变量以#隔开"
                                                       name="form[0][option]">
                                            </td>
                                            <td title="字段编辑页默认值">
                                                <input type="text" class="form-control" placeholder="表单默认值"
                                                       name="form[0][default]">
                                            </td>
                                            <td class="table-text-c" title="勾选后字段显示在列表页中">
                                                <input type="checkbox" id="show_index" name="form[0][show_index]"
                                                       value="1">
                                            </td>
                                            <td title="列表页排序">
                                                <input type="text" class="form-control" readonly
                                                       name="form[0][show_index_sort]"
                                                       onkeyup="this.value=this.value.replace(/[^\d.]/g,'')"
                                                       onpaste="this.value=this.value.replace(/[^\d.]/g,'')">
                                            </td>
                                            <td class="table-text-c" title="勾选后字段显示在添加修改页中">
                                                <input type="checkbox" id="show_addedit" name="form[0][show_addedit]"
                                                       value="1">
                                            </td>
                                            <td title="添加修改页排序">
                                                <input type="text" class="form-control" readonly
                                                       name="form[0][show_addedit_sort]"
                                                       onkeyup="this.value=this.value.replace(/[^\d.]/g,'')"
                                                       onpaste="this.value=this.value.replace(/[^\d.]/g,'')">
                                            </td>
                                            <td class="table-text-c" title="勾选后自动生成控制器筛选项和前端搜索框">
                                                <input type="checkbox" name="form[0][search]" value="1">
                                            </td>
                                            <td title="select的取值为字段中的选项值">
                                                <select class="form-control" name="form[0][search_type]">
                                                    <option value="text">text</option>
                                                    <option value="select">select</option>
                                                    <option value="date">date</option>
                                                </select>
                                            </td>
                                            <td title="勾选后自动给字段添加排序功能">
                                                <input type="checkbox" name="form[0][sort]" value="1">
                                            </td>
                                            <td title="勾选后在验证器和前端生成必填校验规则">
                                                <input type="checkbox" name="form[0][require]" value="1">
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="ibox float-e-margins">
                            <div class="ibox-title">
                                <h5>数据表信息 ：</h5>
                            </div>
                            <div class="ibox-content">
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">数据表：</label>
                                    <div class="radio-box col-sm-4" title="会在数据库自动创建相应模型的数据表">
                                        <div class="checkbox pull-left">
                                            <input type="checkbox" name="create_table" id="radio1" value="1"
                                                   checked="checked">
                                            <label for="radio1">创建数据表</label>
                                        </div>
                                        <div class="checkbox pull-left ml-20"
                                             title="勾选此项后无论表是否存在都会删除原表强制创建新表，不勾选只在表不存在时才创建新表，慎重选择，如果创建失败会自动回滚">
                                            <input type="checkbox" name="create_table_force" id="radio2" value="1">
                                            <label for="radio2">强制建表</label>
                                        </div>
                                    </div>
                                    <div class="input-check col-sm-4">
                                        <button class="btn btn-success btn-sm op-sync" type="button"
                                                title="将表单元素里的字段自动填充到表字段里，会清空原表字段的数据，谨慎操作">
                                            同步字段
                                        </button>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-3"></div>
                                    <div class="col-sm-8">
                                        <span class="text-danger"> 注：勾选强制建表后无论表是否存在都会删除原表强制创建新表，不勾选只在表不存在时才创建新表，慎重选择</span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">存储引擎：</label>
                                    <div class="col-sm-4">
                                        <select name="table_engine" class="form-control">
                                            <option value="MyISAM">MyISAM</option>
                                            <option value="InnoDB">InnoDB</option>
                                            <option value="MRG_MYISAM">MRG_MYISAM</option>
                                            <option value="MEMORY">MEMORY</option>
                                            <option value="ARCHIVE">ARCHIVE</option>
                                        </select>
                                    </div>
                                    <div class="col-sm-4"></div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">表名：</label>
                                    <div class="col-sm-4">
                                        <input class="form-control" type="text" name="table_name" title="不填写由控制器名自动生成"
                                               placeholder="不填写由控制器名自动生成">
                                    </div>
                                    <div class="col-sm-4"></div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-3"></div>
                                    <div class="col-sm-8">
                                        <span class="text-danger"> 注：这里无需设置主键字段，在上方表单中填写主键名即可</span>
                                    </div>
                                </div>
                                <div class="data-table table-responsive">
                                    <table class="table-table table table-bordered table-hover" style="width: 100%;">
                                        <thead>
                                        <tr class="table-text-c">
                                            <th style="width: 190px;" title="删除后不可恢复，谨慎操作">操作<br>
                                                <a href="javascript:;" class="label label-success op-add"
                                                   data-type="field"
                                                   data-header="1">增加一栏</a>
                                            </th>
                                            <th style="width: 120px;" title="只能小写字符和下划线，例如 user_id"><span
                                                    class="text-danger">*</span> 名称
                                            </th>
                                            <th style="width: 120px;"
                                                title="字段类型+大小，例如 varchar(255) , int(10) , text"><span
                                                    class="text-danger">*</span> 类型
                                            </th>
                                            <th style="width: 120px;" title="为NULL表示不设默认值，不区分大小写">默认值</th>
                                            <th style="width: 120px;" title="勾选后生成 NOT NULL">不是 null</th>
                                            <th style="width: 120px;" title="勾选后生成索引">索引</th>
                                            <th style="width: 120px;" title="设置字段备注">备注</th>
                                            <th style="width: 120px;" title="扩展属性，例如 unsigned , auto_increment">扩展属性
                                            </th>
                                        </tr>
                                        </thead>
                                        <tbody id="tbody-field">
                                        <tr class="table-text-c">
                                            <td title="删除后不可恢复，谨慎操作">
                                                <a href="javascript:;" class="label label-success op-add"
                                                   data-type="field">增加一栏</a>
                                                <a href="javascript:;" class="label label-danger op-delete">删除</a>
                                            </td>
                                            <td title="只能小写字符和下划线，例如 user_id">
                                                <input type="text" class="form-control field-name" placeholder="字段名称"
                                                       name="field[0][name]">
                                            </td>
                                            <td title="字段类型+大小，例如 varchar(255) , int(10) , text">
                                                <input type="text" class="form-control" placeholder="例如varchar(255)"
                                                       value="varchar(255)" name="field[0][type]">
                                            </td>
                                            <td title="为NULL表示不设默认值，不区分大小写">
                                                <input type="text" class="form-control" placeholder="为NULL表示不设默认值"
                                                       name="field[0][default]"
                                                       value="NULL">
                                            </td>
                                            <td title="勾选后生成 NOT NULL">
                                                <input type="checkbox" name="field[0][not_null]" value="1">
                                            </td>
                                            <td title="勾选后生成索引">
                                                <input type="checkbox" name="field[0][key]" value="1">
                                            </td>
                                            <td title="设置字段备注">
                                                <input type="text" class="form-control field-comment" placeholder="备注"
                                                       name="field[0][comment]">
                                            </td>
                                            <td title="扩展属性，例如 unsigned , auto_increment">
                                                <input type="text" class="form-control" placeholder="例如unsigned"
                                                       name="field[0][extra]">
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="ibox float-e-margins">
                            <div class="ibox-title">
                                <h5>其他选项</h5>
                            </div>
                            <div class="ibox-content">
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">首页菜单：</label>
                                    <div class="col-sm-4">
                                        <div class="checkbox pull-left">
                                            <input type="checkbox" name="menu[]" id="radio-0" value="add"
                                                   checked="checked">
                                            <label for="radio-0">添加</label>
                                        </div>
                                        <div class="checkbox pull-left ml-20">
                                            <input type="checkbox" name="menu[]" id="radio-3" value="delete_all"
                                                   checked="checked">
                                            <label for="radio-3">删除</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-4"></div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">创建模型：</label>
                                    <div class="col-sm-4">
                                        <div class="checkbox pull-left">
                                            <input type="checkbox" name="model" value="1">
                                            <label title="会自动创建相应的模型，并且自动添加字段create_time，update_time，并且开启时间戳记录">创建模型（model）</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-4"></div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">验证器：</label>
                                    <div class="col-sm-8">
                                        <div class="checkbox pull-left">
                                            <input type="checkbox" name="validate" value="1">
                                            <label title="只自动添加必填规则，其他规则太复杂，会留空，请到验证器手动完善">创建验证器（validate）（默认自动生成前端一致的验证规则）</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-9 col-sm-offset-3">
                                        <button class="btn btn-success" type="submit">
                                            &nbsp;&nbsp;确认创建&nbsp;&nbsp;</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

</div>
{include file="public/footer" /}
<script src="__JS__/jquery-ui-1.10.4.min.js"></script>
<script>
    $(function () {

        $(".db-table").find("[value='{$Request.param.table}']").attr("selected", true);

        $("#tbody-form").sortable();
        $("#tbody-field").sortable();

        // 获取模板
        var template = {}, index = {};
        template['form'] = $("#tbody-form").html();
        template['field'] = $("#tbody-field").html();
        index['form'] = 0;
        index['field'] = 0;

        // 初始化表单验证
        $("#form").validate({
            errorElement: 'span',
            errorPlacement: function (error, element) {
                error.appendTo(getMsgBox(element));
            },
            submitHandler: function (form) {
                $(form).ajaxSubmit(function (result) {
                    console.log(result);
                    if (result.code == 1) {
                        layer.alert('您接下来是要继续生成下一个控制器还是继续停留？', {
                            btn: ['继续生成', '查看', '停留在此页'], //按钮
                            yes: function (i) {
                                location.reload();
                                layer.close(i);
                            },
                            btn2: function (i) {
                                window.open("{:\\think\\Url::build('index')}");
                            }
                        });
                    } else {
                        layer.msg('抱歉，生成未成功');
                    }
                    return false;
                });
            }
        });

        function form_init() {
            /*
             $('.skin-minimal input').iCheck('destroy');
             $('.skin-minimal input').iCheck({
             checkboxClass: 'icheckbox-blue',
             radioClass: 'iradio-blue',
             increaseArea: '20%'
             });
             */
        }

        // 一定要在获取模板后在初始化，否则会出现错误
        form_init();
        // 增加一栏
        $(document).on("click", ".op-add", function () {
            var type = $(this).attr("data-type");
            var html = template[type].replace(/(\[\d+\])/g, '[' + (++index[type]) + ']');
            // 表头菜单，追加到第一个
            if ($(this)[0].hasAttribute('data-header')) {
                $("#tbody-" + type).prepend(html);
            } else {
                $(this).closest('tr').after(html);
            }
            form_init();
        }).on("change", "#show_index", function () {
            if ($(this).is(':checked')) {
                $(this).parent().next().children('input').val('50');
                $(this).parent().next().children('input').removeAttr("readonly");
            } else {
                $(this).parent().next().children('input').val('');
                $(this).parent().next().children('input').attr("readonly", "readonly");
            }
        }).on("change", "#show_addedit", function () {
            if ($(this).is(':checked')) {
                $(this).parent().next().children('input').val('50');
                $(this).parent().next().children('input').removeAttr("readonly");
            } else {
                $(this).parent().next().children('input').val('');
                $(this).parent().next().children('input').attr("readonly", "readonly");
            }
        }).on("click", ".op-delete", function () {
            // 删除一栏
            $(this).closest("tr").fadeOut(undefined, undefined, function () {
                // 使用回调函数，强行移除该DOM
                $(this).remove();
            });
            form_init();
        }).on("change", ".form-require", function () {
            // 必填项自动加*
            var $this = $(this);
            var formValidate = $this.closest("tr").find(".form-validate-datatype");
            var formValidateVal = formValidate.val();
            if ($this.prop('checked')) {
                if (formValidateVal) {
                    if (formValidateVal.substring(0, 1) != "*") {
                        formValidate.val("*|" + formValidateVal);
                    }
                } else {
                    formValidate.val("*");
                }
            } else {
                if (formValidateVal.length == 1) {
                    formValidate.val("");
                } else {
                    formValidate.val(formValidateVal.substring(2));
                }
            }
        }).on("click", ".op-sync", function () {
            // 自动同步
            var objField = $("#tbody-field");
            objField.find('tr').remove();
            $("#tbody-form").find('tr').each(function () {
                objField.append(template['field'].replace(/(\[\d+\])/g, '[' + (++index['field']) + ']'));
                var objCurrent = objField.find('tr:last');
                objCurrent.find('.field-comment').val($(this).find('.form-title').val());
                objCurrent.find('.field-name').val($(this).find('.form-name').val());
            });
            form_init();
        }).on('click', '.db-jump', function () {
            location.href = '{:\\think\\Url::build("index")}?table=' + $('.db-table').val();
        });

        {present name = "table_info"}
        var tableInfo = {$table_info};
        var objForm = $("#tbody-form");
        objForm.find('tr').remove();
        for (var i = 0; i < tableInfo.length; i++) {
            objForm.append(template['form'].replace(/(\[\d+\])/g, '[' + (++index['form']) + ']'));
            var objCurrent = objForm.find('tr:last');
            if (typeof(tableInfo[i]['column_comment']) != "undefined") {
                objCurrent.find('.form-title').val(tableInfo[i]["column_comment"]);
            }
            if (typeof(tableInfo[i]['column_comment']) != "undefined") {
                objCurrent.find('.form-name').val(tableInfo[i]["column_name"]);
            }
            if (typeof(tableInfo[i]['column_comment']) != "undefined" && typeof(tableInfo[i]['character_maximum_length']) != "undefined") {
                objCurrent.find('.form-length').val(tableInfo[i]["character_maximum_length"]);
            }
        }
        {/present}
    })
</script>
</body>
</html>